package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.api.user.UserInfoModer;
import com.stylefeng.guns.api.user.UserModel;
import com.stylefeng.guns.api.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.MoocUserTMapper;
import com.stylefeng.guns.rest.common.persistence.model.MoocUserT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName UserApiImpl
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/8  20:06
 * @Version 1.0
 */
@Component
@Service(interfaceClass = UserApi.class)
public class UserServiceApiImpl implements UserApi {

    @Autowired
    private MoocUserTMapper moocUserTMapper;


    @Override
    public int isLogin(String username, String password) {
        //System.out.println("我是user模块 --> " + username + " : " + password);
        //根据账户密码进行加密
//        String encrypt = MD5Util.encrypt(password);
//        MoocUserT moocUserT = new MoocUserT();
//        moocUserT.setUserName(username);
//        moocUserT.setUserPwd(encrypt);
//        MoocUserT moocUserT1 = moocUserTMapper.selectOne(moocUserT);
//        System.out.println(moocUserT1);
//
//        if (moocUserT1 != null) {
//            return moocUserT.getUuid();
//        }
//        return 0;

        // 根据登陆账号获取数据库信息
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUserName(username);

        MoocUserT result = moocUserTMapper.selectOne(moocUserT);

        // 获取到的结果，然后与加密以后的密码做匹配
        if(result!=null && result.getUuid()>0){
            String md5Password = MD5Util.encrypt(password);
            System.out.println("md5Password --> "+md5Password);
            if(result.getUserPwd().equals(md5Password)){
                return result.getUuid();
            }
        }
        return 0;

    }

    @Override
    public boolean register(UserModel userModel) {
        //获取注册信息
        String username = userModel.getUsername();
        String phone = userModel.getPhone();
        String password = userModel.getPassword();
        String email = userModel.getEmail();
        String address = userModel.getAddress();

        //将注册信息实体转换为数据实体
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUserName(username);
        moocUserT.setUserPhone(phone);
        moocUserT.setEmail(email);
        moocUserT.setAddress(address);
        moocUserT.setBeginTime(new Date());
        moocUserT.setUpdateTime(new Date());

        //数据加密
        String encrypt = MD5Util.encrypt(password);
        moocUserT.setUserPwd(encrypt);
        System.out.println(" 封装的实体 --> " + moocUserT);


        //将数据存入数据库
        Integer insert = moocUserTMapper.insert(moocUserT);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkUsername(String username) {
        EntityWrapper<MoocUserT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("user_name", username);

        Integer result = moocUserTMapper.selectCount(entityWrapper);
        if (result != null && result > 0) {
            return false;
        }
        return true;
    }

    @Override
    public UserInfoModer getUserInfo(int uuid) {
        MoocUserT userT = moocUserTMapper.selectById(uuid);
        return do2UserInfo(userT);
    }

    //入库之后在查出来
    @Override
    public UserInfoModer updateUserInfo(UserInfoModer userInfoModer) {
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUuid(userInfoModer.getUuid());
        moocUserT.setUserSex(userInfoModer.getSex());
        moocUserT.setNickName(userInfoModer.getNickname());
        moocUserT.setLifeState(Integer.parseInt(userInfoModer.getLifeState()));
        moocUserT.setHeadUrl(userInfoModer.getHeadAddress());
        moocUserT.setBirthday(userInfoModer.getBirthday());
        moocUserT.setBiography(userInfoModer.getBiography());
        moocUserT.setEmail(userInfoModer.getEmail());
        moocUserT.setAddress(userInfoModer.getAddress());
        moocUserT.setBeginTime(new Date(System.currentTimeMillis()));
        moocUserT.setUpdateTime(new Date(System.currentTimeMillis()));
        moocUserT.setUserPhone(userInfoModer.getPhone());
        moocUserT.setUserName(userInfoModer.getUsername());

        Integer integer = moocUserTMapper.updateById(moocUserT);
        if (integer > 0) {
          return getUserInfo(moocUserT.getUuid());
        }


        return userInfoModer;
    }


    public UserInfoModer do2UserInfo(MoocUserT userT) {
        UserInfoModer userInfoModer = new UserInfoModer();

        userInfoModer.setUuid(userT.getUuid());
        userInfoModer.setUsername(userT.getUserName());
        userInfoModer.setNickname(userT.getNickName());
        userInfoModer.setEmail(userT.getEmail());
        userInfoModer.setPhone(userT.getUserPhone());
        userInfoModer.setSex(userT.getUserSex());
        userInfoModer.setBirthday(userT.getBirthday());
        userInfoModer.setLifeState(userT.getLifeState() + "");
        userInfoModer.setBiography(userT.getBiography());
        userInfoModer.setAddress(userT.getAddress());
        userInfoModer.setHeadAddress(userT.getHeadUrl());
        userInfoModer.setBeginTime(userT.getBeginTime().getTime());
        userInfoModer.setUpdateTime(userT.getUpdateTime().getTime());
        return userInfoModer;
    }


}
